<?php
namespace PsiFramework\Classes\Util\Database;

import("Classes.Util.Database.Query.SqlTable");
import("Classes.Util.Database.Query.SqlFunction");
import("Classes.Util.Database.Query.SqlColumn");
import("Classes.Util.Database.Query.SqlParameter");
import("Classes.Util.Database.Query.SqlOrder");
import("Classes.Util.Database.Query.SqlLimit");
import("Classes.Util.Database.Query.SqlValue");

use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlParameter;
use \PsiFramework\Classes\Util\Database\Query\SqlOrder;
use \PsiFramework\Classes\Util\Database\Query\SqlJoin;
use \PsiFramework\Classes\Util\Database\Query\SqlLimit;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The SqlQuery class allows to represent a sql query
 *
 * @package Util.Database
 */
class SqlQuery extends SqlParameter
{

    /**
     * @var the "SELECT" operator
     */
    public static /*string*/ $select = "SELECT" ;
    /**
     * @var the "INSERT" operator
     */
    public static /*string*/ $insert = "INSERT" ;
    /**
     * @var the "UPDATE" operator
     */
    public static /*string*/ $update = "UPDATE" ;
    /**
     * @var the "DELETE" operator
     */
    public static /*string*/ $delete = "DELETE" ;
    /**
     * @var the "CREATE" operator
     */
    public static /*string*/ $createTable = "CREATE TABLE" ;
    /**
     * @var the "ALTER TABLE" operator
     */
    public static /*string*/ $alterTable = "ALTER TABLE" ;
    /**
     * @var the "DROP TABLE" operator
     */
    public static /*string*/ $dropTable = "DROP TABLE" ;
    /**
     * @var the "CREATE VIEW" operator
     */
    public static /*string*/ $createView = "CREATE VIEW" ;
    /**
     * @var the "DROP VIEW" operator
     */
    public static /*string*/ $dropView = "DROP VIEW" ;
    /**
     * @var the "CREATE TRIGGER" operator
     */
    public static /*string*/ $createTrigger = "CREATE TRIGGER" ;


    protected /*string*/       $_type ;
    protected /*SqlTable*/     $_table ;
    protected /*SqlJoin[]*/    $_joins ;
    protected /*array*/        $_parameters ;
    protected /*SqlParameter*/ $_whereClause ;
    protected /*array*/        $_groupByClauses ;
    protected /*array*/        $_orderClauses ;
    protected /*SqlLimit*/     $_limit ;

    /**
     * SqlQuery constructor
     *
     * @param $type                the type of the request
     * @param $table               the table concerned by the request
     * @param $joins               the joins for the request
     * @param $parameters          the parameters of the request
     *                             (select columns for a select request,
     *                             the column/value couples for an
     *                             insert or update request)
     * @param $whereClause         the where clause
     * @param $groupByClause       the group by clauses
     * @param $orderClauses        the order clauses
     * @param $limit               the limit clause
     */
    public function __construct(
        /*string*/    $type = null,
        SqlTable      $table = null,
        /*SqlJoin[]*/ $joins=array(),
        /*array*/     $parameters = array(),
        SqlParameter  $whereClause = null,
        /*array*/     $groupByClauses = array(),
        /*array*/     $orderClauses = array(),
        SqlLimit      $limit = null
    ) 
    {
        $this->_type = $type ;
        $this->_table = $table ;
        $this->_joins = $joins ;
        $this->_parameters = $parameters ;
        $this->_whereClause = $whereClause ;
        $this->_groupByClauses = $groupByClauses ;
        $this->_orderClauses = $orderClauses ;
        $this->_limit = $limit ;
    }

    /**
     * Setter for the member variable $type
     *
     * @param $type
     */
    public function setType(/*string*/ $type)
    {
        $this->_type = $type ;
    }

    /**
     * This function allows to set the SqlTable for the request
     *
     * @param $table
     */
    public function setTable(SqlTable $table)
    {
        $this->_table = $table ;
    }

    /**
     * This function allows to add a SqlJoin to the request
     *
     * @param $join
     */
    public function addJoin(SqlJoin $join)
    {
        $this->_joins[] = $join ;
    }

    /**
     * This function sets the joins
     *
     * @param $joins
     */
    public function setJoins(/*SqlJoin[]*/ $joins)
    {
        $this->_joins = $joins ;
    }

    /**
     * This function allows to add a SqlParameter to the request
     *
     * @param $parameter
     */
    public function addParameter(SqlParameter $parameter)
    {
        $this->_parameters[] = $parameter ;
    }

    /**
     * Setter for the member variable $whereClause
     *
     * @param $clause
     */
    public function setWhereClause(SqlParameter $clause)
    {
        $this->_whereClause = $clause ;
    }

    /**
     * This function allows to add a SqlColumn to the
     * group by clause of the request
     *
     * @param $column
     */
    public function addGroupByClause(SqlParameter $column)
    {
        $this->_groupByClauses[] = $column ;
    }

    /**
     * This function allows to add a SqlOrder to the request
     *
     * @param $order
     */
    public function addOrderClause(SqlOrder $order)
    {
        $this->_orderClauses[] = $order ;
    }

    /**
     * Setter for the member variable $limit
     *
     * @param $limit
     */
    public function setLimit(SqlLimit $limit)
    {
        $this->_limit = $limit ;
    }

    /**
     * Getter for the member variable $type
     *
     * @return string
     */
    public function getType()
    {
        return $this->_type ;
    }

    /**
     * Getter for the member variable $table
     *
     * @return SqlTable
     */
    public function getTable()
    {
        return $this->_table ;
    }

    /**
     * Getter for the member variable $joins
     *
     * @return SqlJoin[]
     */
    public function getJoins()
    {
        return $this->_joins ;
    }

    /**
     * Getter for the member variable $parameters
     *
     * @return array of SqlParameters
     */
    public function getParameters()
    {
        return $this->_parameters ;
    }

    /**
     * Getter for the member variable $whereClause
     *
     * @return SqlParameter
     */
    public function getWhereClause()
    {
        return $this->_whereClause ;
    }

    /**
     * Getter for the member variable $groupByClauses
     *
     * @return array of SqlColumns
     */
    public function getGroupByClauses()
    {
        return $this->_groupByClauses ;
    }

    /**
     * Getter for the member variable $orderClauses
     *
     * @return array of SqlOrders
     */
    public function getOrderClauses()
    {
        return $this->_orderClauses ;
    }

    /**
     * Getter for the member variable $limit
     *
     * @return SqlLimit
     */
    public function getLimit()
    {
        return $this->_limit ;
    }

}

